package egovframework.npqs.pqi.mobile.service.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import egovframework.npqs.pqi.mobile.common.JSONResponseUtil;
import egovframework.npqs.pqi.mobile.service.MobileService;
import egovframework.rte.psl.dataaccess.util.EgovMap;

@Controller("mobileController")
public class MobileController {


	@Resource(name="mobileService") 
	private MobileService mobileService;
	
	protected String paramTarget, targetPage, rowPerPage;
	
	@RequestMapping("/index.do")
	public ResponseEntity queryListResult(ModelMap model
				, @RequestParam Map param,HttpServletRequest request,HttpServletResponse response ) throws Exception{
		
		System.out.println("================================service start===========================");
		
		Map paramMap = request.getParameterMap();
		Map<String,Object> map = new HashMap<String,Object>();
		
		paramTarget =""; /*분기 파라미터 REQ014*/
		rowPerPage ="10"; /*페이지 기본 row 갯수*/
		paramTarget = request.getParameter("paramTarget") == null ? "" : request.getParameter("paramTarget");
		System.out.println("paramTarget->"+paramTarget);
		
		String targetPage = "";
		targetPage = request.getParameter("targetPage") == null ? "1":request.getParameter("targetPage");
		
		Iterator<?> it =  paramMap.keySet().iterator();
		
		Map<String,String> requestMap = new HashMap<String,String>();
						
		while( it.hasNext()){
			String key = (String) it.next();
			String[] value = (String[]) paramMap.get(key);
			for( String v : value){
				
				System.out.println("kEY : " + key +":value :"+v);
				requestMap.put(key, v);
			}
			
		}
		//로그인
		if("REQ001".equals(paramTarget))
		{
			System.out.println("궁금해??"+requestMap.toString());
			List<Map<String, String>> list = mobileService.loginSessionContainer(requestMap);
			String result="RES001";
			
			try{
				map.put("result", list.size());
				if(list.size() == 0){
					result = "사용자의 정보가 존재하지 않습니다.";
					map.put("message", result);
				}else{
					result = "로그인에 성공 했습니다.";
					map.put("data", list);
					map.put("message",result);
				}
			
			}catch(Exception e){
				e.printStackTrace();
				result = "시스템 오류입니다.";
				map.put("message", result);
				map.put("result", 0);
			}finally {
				
			}
			
		}
		//회원가입
		else if("REQ002".equals(paramTarget)){
			System.out.println("궁금해??"+requestMap.toString());
			//회원가입
			mobileService.registMember(requestMap);
			//가입후 로그인 정보 가져오기
			List<Map<String, String>> list 
				= mobileService.loginSessionContainer(requestMap);
			
			String result="RES002";
			try{
				
				map.put("result", list.size());
				if(list.size() == 0){
					result = "회원가입 에러.";
					map.put("message", result);
					map.put("data", list);
				}else{
					result = "회원가입에 성공 했습니다.";
					map.put("message",result);
					map.put("data", list);
				}
			
			}catch(Exception e){
				e.printStackTrace();
				result = "시스템 오류입니다.";
				map.put("message", result);
				map.put("result", 0);
			}finally {
				
			}
		}
		//WordList 불러오기
		else if("REQ003".equals(paramTarget)){
			System.out.println("WordList-->"+requestMap.toString());
			requestMap.put("targetPage",targetPage);  /*페이지 target*/
			requestMap.put("rowPerPage", rowPerPage); /*row 수*/
			try{
				//리스트 가져오기
				List<Map<String,String>> list 
					= mobileService.wordList(requestMap);
				String result="RES003"; //WordList 목록
				map.put("result", list.size());
				long totalRows = 0;
				if(list.size() == 0){
					result = "데이터가 존재하지 않습니다.";
					map.put("data", result);
				}else{
					map.put("data", list);
					if(list != null && list.size() > 0 && ((EgovMap)list.get(0)).get("totalRows") != null) {
						totalRows = Long.parseLong(((EgovMap)list.get(0)).get("totalRows").toString());
					}
				}
			}catch(Exception e){
				e.printStackTrace();
				map.put("data", "시스템 오류입니다.");
				map.put("result", 0);
			}finally {
				
			}
		}
		//word Detail 불러오기
		else if("REQ004".equals(paramTarget)){
			System.out.println("WordDetail-->"+requestMap.toString());
			try{
				List<Map<String,String>> list 
					= mobileService.wordDetail(requestMap);
				System.out.println(list.toString());
				String result="RES004"; //WordList 목록
				map.put("result", list.size());
				if(list.size() == 0){
					result = "데이터가 존재하지 않습니다.";
					map.put("message", result);
				}else{
					map.put("message", "성공적으로 데이터를 전송 했습니다.");
					map.put("data", list);
				}
			}catch(Exception e){
				e.printStackTrace();
				map.put("message", "시스템 오류입니다.");
				map.put("result", 0);
			}finally {
				
			}
		}
		//test
		else if("TEST".equals(paramTarget)){
			String master = request.getParameter("resultMaster");
			System.out.println("코드 내용 : "+master);
		}
		//에러~
		else{			
			map.put("result", 0);
			map.put("data", "시스템 오류 입니다.");
		}
		
	   return JSONResponseUtil.getJSONResponse(map);
	   
	}
}